package com.ptkc.ext;

import com.ptkc.core.canal.handler.EventTypeHandler;
import com.ptkc.core.esearch.model.DeliverCost;
import com.ptkc.core.esearch.repos.BusinessRepository;
import com.ptkc.core.esearch.repos.DeliverCostRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: Mr.Jiao
 * @date: 2018年12月15日
 */
@Component
@Slf4j
public class DeliverCostCanalHandler extends EventTypeHandler<DeliverCost> {

    @Autowired
    private DeliverCostRepository deliverCostRepository;

//    @Autowired
//    private BusinessRepository businessRepository;

    public DeliverCostCanalHandler() {
        super("runfast", "runfast_deliver_cost");
    }

    @Override
    public Class<? extends DeliverCost> getEntryClass() {
        return DeliverCost.class;
    }

    @Override
    public void onDelete(DeliverCost rowData) {
        if(rowData != null) {
            try {
                deliverCostRepository.del(rowData.getId());
            } catch (IOException e) {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }
    }

    @Override
    public void onUpdate(DeliverCost rowData) {
        this.onInsert(rowData);
    }

    @Override
    public void onInsert(DeliverCost rowData) {
        if(rowData != null && rowData.getId() != null) {
            try {
                deliverCostRepository.save(rowData);
//                if(rowData.getIsdefault() != null && rowData.getIsdefault().equals(1) && rowData.getAgentId() != null){
//                    businessRepository.setDefaultDeliverCost(rowData.getAgentId());
//                }
            } catch (IOException e) {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }
    }
}
